var keyRepeated = false;
var sampleRow = null;

// Check if Overkill is installed, and get the exe path if it is.

var OverkillPath = null;
try {
	var wsh = new ActiveXObject('WScript.Shell');
	OverkillPath = wsh.RegRead("HKEY_CURRENT_USER\\Software\\ABB\\Overkill\\AppPath");
} catch (e) {}

if (OverkillPath == null) {
	var widget = CreatePopupDialog(""); // not interested in callbacks
	widget.textualContent = "Cannot find Overkill.";
	theTerminal.Push(widget);
	exit();
}

// Check if Overkill is launched

var bspwh = 0;
try {
	bspwh = FindWindow("", "SendKeysOverload");
} catch (e) {
}

if (bspwh == 0) {
	var widget = CreateQuestionDialog("launcher_");
	widget.textualContent = "Launch Overkill?";
	theTerminal.Push(widget);
	exit();
}

launchWidget();
exit();





// Helpers and callbacks

function launchWidget()
{
	var widget = CreateMediaplayerScreen("mykeypad_");
	widget.title = "Overkill";
	widget.name = "Overkill";
	sampleRow = widget.CreateRow("", scCenter, scWrap, scLarge);
	theTerminal.Push(widget);
}


function launcher_OK(w)
{
	if (bspwh == 0)
	{
		new ActiveXObject("Shell.Application").ShellExecute(OverkillPath, "");
	}
	launchWidget();
}


function mykeypad_Update(theScreen)
{
	try {
	
		var bspwh = 0;
		try {
			bspwh = FindWindow("ConsoleWindowClass", "Overkill");
		} catch (e) {}

		if (bspwh == 0) {
			// throw "Overkill not launched";
		}

	} catch (e) {
		if (sampleRow != null) sampleRow.textualContent = e;
		theScreen.mediaLength = -1;
		theScreen.mediaLength = -1;
		theScreen.playerState = scIndeterminate;
	}
}

function mykeypad_KeyDown(theScreen, theKey)
{	
	try {
		var bspwh = FindWindow("", "SendKeysOverload");
		if (bspwh == 0) {
			new ActiveXObject("Shell.Application").ShellExecute(OverkillPath, "");
			throw "null player";
		}

		ActivateWindow(bspwh);
		if (theKey == "2")
		{
			// VK_UP
			SendVirtualKeystroke(0x26, false, false, false, false);
		}
		else if (theKey == "8")
		{
			// VK_DOWN
			SendVirtualKeystroke(0x28, false, false, false, false);
		}
		else if (theKey == "4")
		{
			// VK_LEFT
			SendVirtualKeystroke(0x25, false, false, false, false);
		}
		else if (theKey == "6")
		{
			// VK_RIGHT
			SendVirtualKeystroke(0x27, false, false, false, false);
		}
		else if (theKey == "5")
		{
			// DELETE
			SendVirtualKeystroke(0x2E, false, false, false, false);
		}
		else if (theKey == "*")
		{
			// ESCAPE
			SendVirtualKeystroke(0x1B, false, false, false, false);
		}
		else if (theKey == "1")
		{
			// F1
			SendVirtualKeystroke(0x70, false, false, false, false);
		}
		else if (theKey == "3")
		{
			// F2
			SendVirtualKeystroke(0x71, false, false, false, false);
		}
		else if (theKey == "7")
		{
			// F3
			SendVirtualKeystroke(0x72, false, false, false, false);
		}
		else if (theKey == "9")
		{
			// F4
			SendVirtualKeystroke(0x73, false, false, false, false);
		}
		else if (theKey == "0")
		{
			// F5
			SendVirtualKeystroke(0x74, false, false, false, false);
		}
		else if (theKey == "#")
		{
			// F6
			SendVirtualKeystroke(0x75, false, false, false, false);
		}
	} catch (e) {}

	keyRepeated = false;

	// Keep the keypad active
	return true;
}

function mykeypad_KeyUp(theScreen, theKey)
{	
	try {
		var bspwh = FindWindow("ConsoleWindowClass", "Overkill");
		if (bspwh == 0) throw "null player";

		if (theKey == ":help" || theKey == "#") {
	        showHelp();
		}
	} catch (e) {}
	
	return true;
}

function showHelp()
{
    var code = new Array();
    var title = new Array();
    var description = new Array();
    
    code[0] = "s";
    title[0] = "2 og 8 (Up/Down)";
    description[0] = "Hradi fram og aftur.";

    code[1] = new Array("v", "d");
    title[1] = "4 og 6 (Left/Right)";
    description[1] = "Styri.";

    code[2] = new Array("^", "u");
    title[2] = "1 og 3 (Open/Close)";
    description[2] = "Opna og loka griparmi.";

    code[3] = new Array("<");
    title[3] = "7 og 9 (Raise/Lower)";
    description[3] = "Faera griparm upp og nidur.";

    code[4] = new Array(">");
    title[4] = "5 (Reset)";
    description[4] = "Nullstillir hrada.";

    code[5] = new Array("f", "*");
    title[5] = "* (ESC)";
    description[5] = "Haettir i forriti.";

    theTerminal.ShowKeypadHelp("Overkill Help", code, title, description);    
}